package org.skyfree.ghyll.tcard.print;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Control;

public class PrintHelper 
{
	
	public static void Print(Control control,int direction,ImageData blue,ImageData sketch)
    {
    	Image image = ImageUtil.getImage(control);
  	  	ImageData imageData = image.getImageData();
  	  	Printer printer = new Printer(new PrinterData());
  	    Rectangle trim = printer.computeTrim(0, 0, 0, 0);
  		if (printer.startJob("test")) 
  		{
  			if (printer.startPage()) {
  				GC gc = new GC(printer);
  				imageData = rotate(imageData,direction);
  				Image printerImage = new Image(printer, imageData);
  				Rectangle big = printer.getBounds();
  				gc.drawImage(
  					printerImage,
  					0,
  					0,
  					imageData.width,
  					imageData.height,
  					-trim.x,
  					-trim.y,
  					(big.width-trim.width+2*trim.x),
  					(big.height-trim.height+2*trim.y));
  				printerImage.dispose();
  				printer.endPage();
  				gc.dispose();
  			}
  			if ((blue!=null)&&printer.startPage()) {
  				GC gc = new GC(printer);
  				ImageData blutPrint = rotate(blue,SWT.RIGHT);
  				Image printerImage = new Image(printer, blutPrint);
  				Rectangle big = printer.getBounds();
  				gc.drawImage(
  					printerImage,
  					0,
  					0,
  					blutPrint.width,
  					blutPrint.height,
  					-trim.x,
  					//-trim.x+(int)(10*scaleFactor),
  					-trim.y,
  					(big.width-trim.width+2*trim.x),
  					(big.height-trim.height+2*trim.y));
  				printerImage.dispose();
  				gc.dispose();
  				printer.endPage();
  			}
  			if ((sketch!=null)&&printer.startPage()) {
  				GC gc = new GC(printer);
  				ImageData sketchPrint = rotate(sketch,SWT.RIGHT);
  				Image printerImage = new Image(printer, sketchPrint);
  				Rectangle big = printer.getBounds();
  				gc.drawImage(printerImage,0,0,sketchPrint.width,sketchPrint.height,
  						-trim.x,-trim.y,(big.width-trim.width+2*trim.x),(big.height-trim.height+2*trim.y));
  				printerImage.dispose();
  				gc.dispose();
  				printer.endPage();
  				
  			}
  			
  		}
  		printer.endJob();
  		printer.dispose();
    }

    static ImageData rotate(ImageData srcData, int direction) {
    	
    	int bytesPerPixel = srcData.bytesPerLine / srcData.width;
    	int destBytesPerLine = (direction == SWT.DOWN)? srcData.width * bytesPerPixel : srcData.height * bytesPerPixel;
    	byte[] newData = new byte[(direction == SWT.DOWN)? srcData.height * destBytesPerLine : srcData.width * destBytesPerLine];
    	int width = 0, height = 0;
    	for (int srcY = 0; srcY < srcData.height; srcY++) {
    		for (int srcX = 0; srcX < srcData.width; srcX++) {
    			int destX = 0, destY = 0, destIndex = 0, srcIndex = 0;
    			switch (direction){
    				case SWT.LEFT: // left 90 degrees
    					destX = srcY;
    					destY = srcData.width - srcX - 1;
    					width = srcData.height;
    					height = srcData.width; 
    					break;
    				case SWT.RIGHT: // right 90 degrees
    					destX = srcData.height - srcY - 1;
    					destY = srcX;
    					width = srcData.height;
    					height = srcData.width; 
    					break;
    				case SWT.DOWN: // 180 degrees
    					destX = srcData.width - srcX - 1;
    					destY = srcData.height - srcY - 1;
    					width = srcData.width;
    					height = srcData.height; 
    					break;
    			}
    			destIndex = (destY * destBytesPerLine) + (destX * bytesPerPixel);
    			srcIndex = (srcY * srcData.bytesPerLine) + (srcX * bytesPerPixel);
    			System.arraycopy(srcData.data, srcIndex, newData, destIndex, bytesPerPixel);
    		}
    	}
    	// destBytesPerLine is used as scanlinePad to ensure that no padding is required
    	return new ImageData(width, height, srcData.depth, srcData.palette, destBytesPerLine, newData);
    }
}
